package info.niwota.frames.net;

import info.niwota.frames.Debug;

import java.io.InputStream;
import java.io.OutputStream;

import android.util.Log;

public class CopyThread implements Runnable {
	private static final String TAG = "CopyThread";

	private static final int BUF_SIZE = 8192;
	
	InputStream in;
	OutputStream out;

	public CopyThread(InputStream in, OutputStream out) {
		this.in = in;
		this.out = out;
	}

	public void run() {
		try {
			byte[] buf = new byte[BUF_SIZE];
			
			while (true) {
				int len = in.read(buf);
				if (len == -1) {
					break;
				}
				if (len > 0) {
					out.write(buf, 0, len);
					out.flush();
					
					if (Debug.DEBUG) {
						Log.d(TAG, "->" + len);
					}
				}
			}
		} catch (Exception e) {
			Log.d(TAG, e.getMessage() + "");
		} finally {
			try {
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}
